﻿#include "qwe.h"
#include <QDebug>

qwe::qwe(QWidget *parent) : QLabel(parent)
{
    double max = 70;
    double jianju = 2;
    double font = 10;
    this->setStyleSheet("background-color: rgb(100, 100, 140);");

    m_startAngle = 180; //顺时针角度
    m_endAngle = 0;

    //qDebug()<<"123"<<a<<b;
    m_minSpeed = 0;   //由于绘制刻度，需要为5倍数  最小值
    m_maxSpeed = max*10;
    m_curSpeed = m_minSpeed;//当前值
    step = (max - m_minSpeed)*10 / jianju/10;
    //qDebug()<<step;
    angleStep =(140.0 - m_endAngle) / step;
    //qDebug()<<angleStep;

    kd_cs=jianju*10;
    kd_cs2=font*10;
    kd_cs3=jianju*10;

    font_cs=jianju*10;
    font_cs1=font*10;

    kd=0.0;
}

void qwe::paintEvent(QPaintEvent *)
{
    a=rect().width();
    b=rect().height();
    //qDebug()<<b;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);// 抗锯齿
    a=rect().width(); //绘制的参考大小
    b=rect().height();
    double m_refSize=a;
    double m_rehSize=b;
    //qDebug()<<"345"<<a<<b;
    painter.scale(rect().width()/m_refSize,rect().height()/m_rehSize);
    painter.translate(m_refSize/2,m_rehSize);//移动起点坐标
    drawFrame(painter,m_refSize,m_rehSize);
    drawNumberSpeed(painter);//显示数字速度
    drawDividing(painter,kd_cs,kd_cs2,kd_cs3);//绘制刻度
    drawNumberIndicator(painter,font_cs,font_cs1);//绘制指示数字
    drawIndicator(painter);//绘制速度指针
}

void qwe::setRange(double max,double jianju,double font)
{
    m_maxSpeed = max*10;
    kd_cs=jianju*10;
    kd_cs2=font*10;
    kd_cs3=jianju*10;

    font_cs=jianju*10;
    font_cs1=font*10;
    step = (max - m_minSpeed)*10 / jianju/10;
    //qDebug()<<step;
    angleStep =(140.0 - m_endAngle) / step;
    update();
}

void qwe::setCurentSpeed(float speed)
{
    if(speed>m_maxSpeed/10||speed<0) {
        qDebug()<<"指针数据错误";
        return;
    }
    //qDebug()<<"speed" << speed;
    kd=speed/kd_cs*10*angleStep;
    update();
}

void qwe::setTitle(const QString &titled)
{

}

void qwe::drawFrame(QPainter &painter,double a,double b)
{
    painter.save();
    painter.setPen(Qt::NoPen);//确保没有边框线----填满，不留边界线
    //绘制灰色圆形外边框
    QLinearGradient lg1(-m_radius,0,0,m_radius);//渐变区域
    lg1.setColorAt(0,QColor(7,223,104));
    lg1.setColorAt(0.5,QColor(7,223,104));
    lg1.setColorAt(1,Qt::gray);
    painter.setBrush(QColor(245,245,245));//设置画笔
    painter.drawRect(-a/2,0,a,-b);
    painter.restore();

}

//绘制刻度
void qwe::drawDividing(QPainter &painter,int cs1,int cs2,int cs3)
{
    painter.save();
    painter.rotate(110);//将坐标系顺时针旋转150°，到达起始位置

    QPen pen(QColor(100,100,140));
    painter.setPen(pen);

    //for (int i = m_minSpeed; i <= m_maxSpeed; i += 2)
    for (int i = m_minSpeed; i <= m_maxSpeed; i += cs1)
    {
        if (i >= m_maxSpeed){ //绘制红色
            pen.setColor(QColor(7,223,104));
            painter.setPen(pen);
        }

        if (i % cs2 == 0){//粗线
            pen.setWidth(2);
            painter.setPen(pen);
            painter.drawLine(0,b-b/10,0,b-b/2);

        }else if (i % cs3 == 0){//中等
            pen.setWidth(1);
            painter.setPen(pen);
            painter.drawLine(0,b-b/10*3,0,b-b/2);

        }
        painter.rotate(angleStep);
    }

    painter.restore();
}

//绘制数字指示
void qwe::drawNumberIndicator(QPainter &painter,int cs1,int cs2)
{
    painter.save();
    painter.rotate(110);
    QPen pen(QColor(100,100,140));
    painter.setPen(pen);
    int bs=rect().height();
    painter.setFont(QFont("宋体",bs/12,1));

    for (int i = m_minSpeed; i <= m_maxSpeed; i+= cs1)//每隔20Km设置一个数字
    {

        QString speed = QString::number(i/10);
        if (i % cs2 == 0)
        {
            painter.save();
            painter.translate(0,b-b/10);
            painter.rotate(180);
            painter.drawText(QPointF(0,10),speed);
            painter.restore();
        }
        painter.rotate(angleStep);
    }

    painter.restore();
}

void qwe::drawIndicator(QPainter &painter)
{
    //绘制指针
    painter.save();
    painter.rotate(200+kd); //旋转坐标系

    QRadialGradient haloGradient(0, 0, b/2, 0, 0);  //辐射渐变
    haloGradient.setColorAt(0, QColor(255,255,255));
    haloGradient.setColorAt(1, QColor(0,0,0)); //灰
    painter.setPen(QColor(7,223,104)); //定义线条文本颜色  设置线条的颜色//Qt::transparent
    painter.setBrush(haloGradient);//刷子定义形状如何填满 填充后的颜色
    int as=rect().width();
    int bs=rect().height();
    //qDebug()<<bs;
    QPointF points[3] = {
        QPointF(0.0, bs/32),
        QPointF(0.0, -bs/32),
        QPointF(bs/2+bs/5, 0),
    };
    painter.drawPolygon(points,3); //绘制三角形指针
    painter.restore();


    //绘制旋转中心
    painter.save();
    QRadialGradient rg(0,0,bs/9,0,0);
    rg.setColorAt(1.0,Qt::gray);
    rg.setColorAt(0.2,QColor(100,100,140));
    painter.setPen(Qt::NoPen);
    painter.setBrush(rg);
    painter.drawEllipse(QPoint(0,0),bs/19,bs/19);
    painter.restore();
}

void qwe::drawNumberSpeed(QPainter &painter)
{
    painter.save();
    painter.setPen(QColor(100,100,140));
    int bs=rect().height();
    painter.setFont(QFont("宋体",bs/10,1));

    painter.drawText(QPointF(-bs/10,-bs/3),"Kw");
    painter.restore();
}
